const utils = require('../../utils.js')
const qcloud = require('../../vendor/qcloud-weapp-client-sdk/index.js')
const config = require('../../config.js')

Component({
  properties: {
    state: Boolean,
    classDate: {
      type: Object,
      value: {},
      observer(newValue, oldValue) {
        if (newValue.name != '') {
          this.getobtainNotices()
        }
      }
    }
  },
  data: {
    title:"",
    setHeight: 0,
    noticesList:[],
    lockState:false,
    curIndex:0
  },
  methods: {
    clickFn(){
      
      this.readNotice(this.data.noticesList[this.data.curIndex].cnId)

      this.setData({
        curIndex: this.data.curIndex + 1
      })

      

      if (this.data.curIndex > this.data.noticesList.length-1) {
        this.setData({
          lockState: false
        })

        return false;  
      }

      this.setData({
        title: this.data.noticesList[this.data.curIndex].ciId == 0 ? '全体公告' : '班级公告'
      })
    },

    readNotice(id) {
      utils.ajaxFn({
        url: config.service.readNotice,
        body: {
          cnId: id
        },
        callback(){

        }
      })
    },
    
    // 获取未读公告列表
    getobtainNotices(){
      utils.ajaxFn({
        url: config.service.obtainNoticesUnreaded,
        body: {
          ciId: this.data.classDate['ciId']
        },
        callback: (data) => {
          this.setData({
            noticesList: data.noticeDTOs
          })

          if (this.data.noticesList.length > 0) {
            this.setData({
              lockState: true
            })

            this.setData({
              title: this.data.noticesList[0].ciId == 0 ? '全体公告' : '班级公告'
            })

          }
          
          
          
        }
      })
    }
  },

  attached(opts) {
    
  }
})